package com.bhs.sansonglogistics.ui.order;

import static android.content.Context.CLIPBOARD_SERVICE;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseFragment;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.bean.OrderGoodsDataBean;
import com.bhs.sansonglogistics.bean.OrderGoodsBean;
import com.bhs.sansonglogistics.dialog.QuotedPriceDialog;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.adapter.OrderGoodsAdapter;
import com.bhs.sansonglogistics.ui.home.SourceGoodsListFragment;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 报价记录
 */
public class HistoryOrderListFragment extends BaseFragment implements NetCallBack {

    private ClipData mClipData;   //剪切板Data对象
    private ClipboardManager mClipboardManager;   //剪切板管理工具类
    private int type;
    private int page = 1;
    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private OrderGoodsAdapter orderGoodsAdapter;

    public static HistoryOrderListFragment newInstance(int type) {
        HistoryOrderListFragment fragment = new HistoryOrderListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
        }
    }

    @Override
    public int initLayout() {
        return R.layout.fragment_history_order_list;
    }

    @Override
    public void initView(View root) {
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        mRvList = root.findViewById(R.id.rv_list);
        mRefreshLayout = root.findViewById(R.id.refreshLayout);
        // 1. 注册mClipboardManager
        mClipboardManager = (ClipboardManager) getActivity().getSystemService(CLIPBOARD_SERVICE);

        //监听 刷新是回调
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });
    }

    @Override
    public void initData() {
        loadData();
        orderGoodsAdapter = new OrderGoodsAdapter(type);
        mRvList.setAdapter(orderGoodsAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(getContext()));
        orderGoodsAdapter.setEmptyView(LayoutInflater.from(getContext()).inflate(R.layout.layout_empty, null));
//        orderGoodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                if (type == 5) {
//                    OrderGoodsBean data = (OrderGoodsBean) adapter.getData().get(position);
//                    startActivity(new Intent(getActivity(), OrderDetailsActivity.class)
//                            .putExtra("deliver_sn", data.getDeliver_sn()));
//                }
//            }
//        });

        orderGoodsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);

        orderGoodsAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                OrderGoodsBean data = (OrderGoodsBean) adapter.getData().get(position);
                switch (view.getId()) {
                    case R.id.tv_order_number:
                        //创建一个新的文本clip对象
                        mClipData = ClipData.newPlainText("Simple text", data.getDeliver_sn());
                        //把clip对象放在剪贴板中
                        mClipboardManager.setPrimaryClip(mClipData);
                        ToastUtil.show("复制成功");
                        break;
                    case R.id.btn_check_offer:
                        data.setQuoted_create_time(data.getQuoted_time());
                        //报价
                        QuotedPriceDialog quotedPriceDialog = new QuotedPriceDialog(HistoryOrderListFragment.this, null, data);

                        new XPopup.Builder(getContext())
                                .asCustom(quotedPriceDialog)
                                .show();
                        break;
                }

            }
        });


    }

    private Map<String, String> parameters = new ArrayMap<>();

    private void loadData() {
        if (type == 4) {
            networkRequest(netApi.getQuotedPriceList(page, 10), this);
        } else {
            parameters.put("status", "80");
            parameters.put("page", String.valueOf(page));
            networkRequest(netApi.getOrderList2(parameters), this);
        }
    }


    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        mRefreshLayout.setRefreshing(false);
        OrderGoodsDataBean dataBean = new Gson().fromJson(json, OrderGoodsDataBean.class);
        if (dataBean.isStatus()) {
            if (page == 1) {
                orderGoodsAdapter.setNewData(dataBean.getData().getList());
            } else {
                orderGoodsAdapter.loadMoreComplete();
                orderGoodsAdapter.addData(dataBean.getData().getList());
            }
            if (orderGoodsAdapter.getData().size() >= dataBean.getData().getTotal_count()) {
                orderGoodsAdapter.loadMoreEnd();
            }
        } else {
            ToastUtil.show(dataBean.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        orderGoodsAdapter.loadMoreComplete();
    }
}